<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	
</body>
</html>
<script>
	var arr = [2, 3, 4, 5, 6];
	//修改数组中的数据
	arr[0] = 5;
	console.log(arr);

	//稀疏数组
	arr = [1,,2,,];
	console.log(arr.length);//长度为4
	//如果数组中元素不存在，值为undefined
	console.log(arr[1]);//数组序号从零开始

    //求和
	arr = [5, 4, 3, 6, 8, 9];
	var sum = 0;
	for (var i = 0; i <= arr.length; i++){
		sum += arr[i];
	}
       document.write("sum = " + sum); 


       var min = arr[0];
       var max = arr[0];
       for (var i = 1; i <= arr.length; i++){
       	   min = min < arr[i] ? min : arr[i];
       	   max = max < arr[i] ? max : arr[i];
       	   
       } 
       document.write("<hr>");
       document.write("最小值：" + min);
       

       //冒号排序
    /*   arr = [5, 4, 3, 2, 1];

       //第一轮
       if (arr[0] > arr[1]){
       	  var temp = arr[0];
       	  arr[0] = arr[1];
       	  arr[1] = temp;
       }
       console.log(arr);

       //第二轮
       if (arr[1] > arr[2]){
           var temp = arr[1];
           arr[1] = arr[2];
           arr[2] = temp;
       }
       console.log(arr);

       //
       if (arr[2] > arr[3]){
       	var temp = arr[2];
       	arr[2] = arr[3];
       	arr[3] = temp;
       }
       console.log(arr);

       if (arr[3] > arr[4]){
       	var temp = arr[3];
       	arr[3] = arr[4];
       	arr[4] = temp;
       }
       console.log(arr);
*/
      arr = [5, 4, 3, 2, 1];
     for (var i = 0; i <= arr.length-1; i++){
     	    if(arr[i] > arr[i+1]){
     	    	var temp = arr[i];
     	    	arr[i] = arr[i+1];
     	    	arr[i+1] = temp; 
     	    }
     }
     console.log(arr);
     for (var i = 0; i <= arr.length-1-1; i++){
     	    if(arr[i] > arr[i+1]){
     	    	var temp = arr[i];
     	    	arr[i] = arr[i+1];
     	    	arr[i+1] = temp; 
     	    }
     }
     console.log(arr);

       for (var i = 0;i <= arr.length-1-2; i++){
       	    if(arr[i] > arr[i+1]){
       	    	var temp = arr[i];
       	    	arr[i] = arr[i+1];
       	    	arr[i+1] = temp;
       	    } 
       }
       console.log(arr);

       for (i = 0;i <= arr.length-1-3; i++){
       	    if(arr[i] > arr[i+i]){
       	    	var temp = arr[i];
       	    	arr[i] = arr[i+1];
       	    	arr[i+1] = temp;
       	    }
       	    }       	   
       	     console.log(arr);


       	     // for(i = 0; i<= arr.length-1-4){
       	     // 	if(arr[i] > arr[i+1]){
       	     // 		var temp = arr[i];
       	     // 		arr[i] = arr[i+1];
       	     // 		arr[i+1] = temp;
       	     // 	}
       	     // }
       	     // console.log(arr);

              //外层控制比较的轮数
              //内层控制每轮比较的次数
             for (var j = 0; j <= arr.length-1; j++){
             	for(var i = 0; i <= arr.length-1-j; i++){
             		if(arr[i] > arr[i+1]) {
             			var temp = arr[i];
             			arr[i] = arr[i+1];
             			arr[i+1] = temp; 
             		}
             	}
             }
             console.log(arr);





</script>